package com.acme.domain.client;

import com.acme.domain.account.Account;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Client implements Serializable {
    private final String name;
    private final Gender gender;

    private List<Account> accounts = new ArrayList<Account>();

    public Client(final String name, final Gender gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public String getSalutation() {
        return gender.getSalutation() + " " + name;
    }

    public Account getAccount(final int id) {
        for (Account account : accounts) {
            if (account.getAccountNumber() == id) {
                return account;
            }
        }
        return null;
    }

    public void addAccount(final Account account) {
        accounts.add(account);
    }

    public List<Account> getAccountsList() {
        return Collections.unmodifiableList(accounts);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (accounts != null ? !accounts.equals(client.accounts) : client.accounts != null) return false;
        if (gender != client.gender) return false;
        if (name != null ? !name.equals(client.name) : client.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (gender != null ? gender.hashCode() : 0);
        result = 31 * result + (accounts != null ? accounts.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return new StringBuilder("Client{")
                .append("name='").append(name).append("\'")
                .append(", gender=").append(gender)
                .append(", accounts=").append(accounts)
                .append("}")
                .toString();
    }
}